import React from 'react';
import { Redirect, useLocation } from 'umi';

function useAuth() {
  const token = sessionStorage.getItem('token');
  return !!token;
}

// 路由鉴权
export default (props: any) => {
  const isLogin = useAuth();
  const location = useLocation();

  if (location.pathname === '/login') {
    console.log(isLogin);
    if (isLogin) {
      return <Redirect to={'/dashboard'} />;
    } else {
      return <>{props.children}</>;
    }
  } else {
    if (isLogin) return <>{props.children}</>;
    return <Redirect to={'/login'} />;
  }
};

// 笔记 类比于路由钩子函数，用来做路由鉴权
